import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: XXHH
 * @CreateTime: 2025-08-21
 */
public class Demo53 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(10);
        ExecutorService executor = Executors.newScheduledThreadPool(4);
        for (int i = 0;i<10;i++){
            int id = i;
            executor.submit(()->{
                System.out.println("子任务开始:"+id);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("子任务结束:"+id);
                latch.countDown(); // 完成一个 用一次
            });
        }
        latch.await(); // 阻塞等待所有任务结束
        System.out.println("子任务完成");
        executor.shutdown();
    }
}
